import {_decorator, Animation, Component, director, macro, Node, Tween, tween, Vec2, Vec3} from 'cc';
import {lobby} from "db://assets/script/lobby";
import {FishDoSomethingEvent} from "db://assets/script/common/events/fish-events/fish-do-something-event";
import {FishShow} from "db://assets/script/game/fish/fish-show";
import {FishGameAction} from "db://assets/script/game/constants/fish-game-action";
import {FishFryDetail} from "db://assets/proto/shared/v1/wuni";
import {FishFryInfoComp} from "db://assets/script/game/fish/fish-fry-info-comp";
import LabelTime from "db://assets/script/common/label-time";
import {serverTimeHelper} from "db://assets/script/server-time-helper";
import {GameEvent} from "db://assets/script/game-event";
import {
    UserFishFryTakeCompleteEvent
} from "db://assets/script/common/events/fish-events/user-fish-fry-take-complete-event";
import {FishGameStatus} from "db://assets/script/game/constants/fish-game-status";
import {
    UserFishFryRefuseCompleteEvent
} from "db://assets/script/common/events/fish-events/user-fish-fry-refuse-complete-event";

const {ccclass, property} = _decorator;

@ccclass('FishFryInCity')
export class FishFryInCity extends Component {


    @property(FishShow)
    fishShow: FishShow = null!;

    fishFry: FishFryDetail | null = null;

    @property(LabelTime)
    leaveTime!: LabelTime;

    gameAction: FishGameAction = FishGameAction.NOTING


    start() {
        this.onEvent()
    }

    init(fishFry: FishFryDetail): void {
        this.fishFry = fishFry
        const nowMs = serverTimeHelper.loadServerTime()
        if (fishFry.leaveAtMs.toNumber() - nowMs <= 10 * 60 * 1000 && fishFry.leaveAtMs.neq(-1)) {
            this.leaveTime.setTimeByServerTime(fishFry.leaveAtMs.toNumber())
            this.leaveTime.onComplete = () => {
                this.destroy()
            }
            this.leaveTime.prefixText = '离开：'
            this.leaveTime.node.active = true
        }
        this.fishShow.init(this.fishFry.fishId, 1);
        this.onEvent()
    }

    onEvent() {
        this.node.on(Node.EventType.TOUCH_END, this._seeMine, this)
        director.on(GameEvent.userFishFryTakeComplete, this.takeComplete, this)
        director.on(GameEvent.userFishFryRefuseComplete, this.refuseComplete, this)
    }

    offEvent() {
        director.off(GameEvent.userFishFryTakeComplete, this.takeComplete, this)
        director.off(GameEvent.userFishFryRefuseComplete, this.refuseComplete, this)
    }

    walkTo(offsetX: number, offsetY: number, duration: number, action: FishGameAction) {
        const moveClip = this.calcMoveClipByTargetPos(offsetX, offsetY, duration)
        this.fishShow.notificationPlay(new FishDoSomethingEvent(action,
                () => FishGameStatus.EMPTY,
                moveClip,
                macro.REPEAT_FOREVER,
                () => {
                },
                () => new Vec3(offsetX, offsetY),
                duration,
                () => {
                    this.gameAction = FishGameAction.NOTING
                    this.fishShow.getComponent(Animation)!.play(this.fishShow.idleName())
                },
                0.3
            )
        )
    }

    moveTo(offsetX: number, offsetY: number, duration: number) {
        const originPos = this.node.position;
        const originV2Pos = new Vec2()
        originV2Pos.x = originPos.x;
        originV2Pos.y = originPos.y;
        const vec = new Vec3(originPos.x + offsetX, originPos.y + offsetY, originPos.z)
        this.fishShow.node.setScale(Math.abs(this.fishShow.node.scale.x) * (offsetX < 0 ? -1 : 1), this.fishShow.node.scale.y)
        const fishTweenTag = this.fishFry!.fishId.toNumber()
        Tween.stopAllByTag(fishTweenTag)
        // 使用缓动动画
        return tween(this.node)
            .tag(1000)
            .tag(fishTweenTag)
            .to(duration, {position: vec})
    }

    calcMoveClipByTargetPos(offsetX: number, offsetY: number, duration: number) {
        const distance = Vec2.distance(new Vec2(0, 0), new Vec2(offsetX, offsetY))
        if ((distance / duration) < 300) {
            return 'walk-1'
        }
        return 'running-1'
    }

    _seeMine() {
        const fryInfo = lobby.instance.addUIWithClickMask('prefab/fish-fry-info', 128, (node) => node.destroy())
        fryInfo.getComponent(FishFryInfoComp)!.init(this.fishFry!)
    }

    takeComplete(event: UserFishFryTakeCompleteEvent) {
        if (this.fishFry!.fishId.eq(event.fishFry.fishId)) {
            this.node.destroy()
        }
    }

    refuseComplete(event: UserFishFryRefuseCompleteEvent) {
        if (this.fishFry!.fishId.eq(event.fishId)) {
            this.node.destroy()
        }
    }


    protected onDestroy(): void {
        this.offEvent()
    }

    _updateUI(fishId: string) {
    }


    update(deltaTime: number) {

    }
}


